package com.controller.qm;

import java.io.IOException;
import java.net.URLEncoder;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.StringUtils;
import com.dao.qm.PartsDao;
import com.service.qm.PartsDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.service.qm.PartsService;
import com.model.qm.PartsModel;

/**
 * 备件配件验收
 *
 * @author 10961
 */
@Controller
@RequestMapping(value = "/api")
public class PartsController {
    static final Logger logger = LoggerFactory.getLogger(PartsController.class);

    @Autowired
    PartsService partsService;
    @Autowired
    PartsDetailService partsDetailService;
    @Autowired
    PartsDao partsDao;

    /*	*/

    /**
     * 查询列表
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "qm/parts/selectList", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult selectList(@RequestParam Map<String, Object> map) {
        return partsService.selectList(map);
    }

    /**
     * 查询列表
     *
     * @param request
     * @return
     */
   /* @RequestMapping(value = "/wx/qm/index")
    public String indexList(HttpServletRequest request) {
        System.out.println("11111");
        Map<String, Object> params = new HashMap<>();
        String pageTem = request.getParameter("page").toString();
        String rowsTem = request.getParameter("rows").toString();
        if (pageTem != null) {
            int rows = Integer.parseInt(rowsTem);
            int page = Integer.parseInt(pageTem);
            int pageNum = (page - 1) * rows;
            params.put("pageNum", pageNum);
            params.put("pageRows", rows);
        }
        List<PartsModel> list = partsDao.selectList(params);
        int count = partsDao.selectListCount(params);
        request.setAttribute("list", list);
        request.setAttribute("count", count);
        return "wx/qm/index";
    }
*/

    /**
     * 查询详情
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/parts/selectInfor", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult selectInfor(@RequestParam String parId) {
        return partsService.selectInfor(parId);
    }

    /**
     * 保存询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/parts/save", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult save(@RequestBody PartsModel partsModel) {
        return partsService.save(partsModel);
    }

    /**
     * 删除询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/parts/delete", method = {RequestMethod.DELETE})
    @ResponseBody
    public HttpResult delete(@RequestParam String parId) {
        return partsService.delete(parId);
    }

    /**
     * 导出
     *
     * @param map
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "qm/parts/export")
    public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        partsService.export(map, response);

    }

    @RequestMapping(value = "qm/parts/getPartsOptions", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult getPartsOptions(@RequestParam String type) {
        if (StringUtils.isNotNull(type)) {
            return HttpResult.ok(partsDetailService.getPartsDetailOptions(type));
        }
        return HttpResult.error("请输入配置类型");

    }

    /**
     * 提交至SAP
     *
     * @param
     * @return
     */
    @RequestMapping(value = "qm/parts/commit", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult commit(@RequestBody PartsModel partsModel) throws ServerException {
        return partsService.commit(partsModel);
    }

}
